<template>
  <el-dialog :visible.sync="dialogShow" :title="title" @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch
          v-model="form.enVisible"
          active-value="1"
          inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
      <div style="text-align:center;">
        <el-button type="primary" size="small" @click="save">确定</el-button>
        <el-button size="small">取消</el-button>
      </div>
    </el-form>
  </el-dialog>
</template>

<script>
import { addPermAPI, detailPermAPI, editPermAPI } from '@/api/permissions'
export default {
  data() {
    return {
      title: '新增权限点',
      dialogShow: false,
      form: {
        name: '',
        code: '',
        description: '',
        enVisible: true,
        type: 1,
        pid: '0'
      },
      rules: {
        name: [
          { required: true, message: '请输入权限名', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入权限标识', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    close() {
      this.form = {
        name: '',
        code: '',
        description: '',
        enVisible: true,
        type: 1,
        pid: '0'
      }
      this.$refs.form.clearValidate()
    },
    save() {
      // 做整个表单校验
      this.$refs.form.validate(async v => {
        if (v) {
          if (this.title === '添加权限点') {
          // 发请求
            await addPermAPI(this.form)
            this.$message.success('添加成功')
          } else {
            await editPermAPI(this.form)
            this.$message.success('成功')
          }
          // 刷新数据
          this.$emit('reload')
          // 隐藏对话框
          this.dialogShow = false
        }
      })
    },
    // 显示自己的方法
    show(type, pid) {
      this.title = '添加权限点'
      // 把传递过来的type和pid存起来
      this.form.type = type
      this.form.pid = pid
      this.dialogShow = true
    },
    async editShow(id) {
      this.title = '编辑权限点'
      // 获取被点的内容，然后发请求
      const res = await detailPermAPI(id)
      // 回显给form
      this.form = res.data
      // 显示出对话框
      this.dialogShow = true
    }
  }
}
</script>

<style>

</style>
